package sendmail;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.Properties;

/**
 * Hello world!
 */
public class App {
    //天气接口api Key
    private final static String apiKey = "4a417aba564b4f6a82ba71ab864a3035&cityname=郫县";

    /**
     * 主程序
     *
     * @param
     * @throws Exception
     */
    public static void get() throws Exception {
        //final long time = 1000;
        //用okHttp调用天气接口
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://api.avatardata.cn/Weather/Query?key=" + apiKey)
                .build();
        Response response = client.newCall(request).execute();//返回结果
        //System.out.println(response.body().string());

        //处理返回结果，找到最短的笑话，因为139邮箱的短信提示最多显示140个字
        //JSONObject shortJoke = null;
        String realTimeWeather = "";
        if (response.isSuccessful()) {
            //将返回结果转换为Json对象（返回结果为json格式，具体返回参数
            // 请参见接口文档http://apistore.baidu.com/apiworks/servicedetail/864.html）
            JSONObject resp = JSON.parseObject(response.body().string());
            System.out.println(resp);
            //String st=JSON.toJSONString(resp);
            JSONObject result = resp.getJSONObject("result");
            JSONObject realTime = result.getJSONObject("realtime");
            JSONObject weather = realTime.getJSONObject("weather");

            realTimeWeather = "日期：" + realTime.getString("date") +
                    "\n农历：" + realTime.getString("moon") +
                    "\n星期：" + realTime.getString("week") +
                    "\n地点：" + realTime.getString("city_name") +
                    "\n天气：" + weather.getString("info") +
                    "\n温度：" + weather.getString("temperature") +
                    "\n湿度：" + weather.getString("humidity");
//

            //JSONArray contentList = resp.getJSONArray("result");
//
//                //遍历查找最短的笑话
//                int min = 10000;
//                try {
//                for (int j = 0; j < contentList.size(); j++) {
//                    JSONObject one = contentList.getJSONObject(j);
//                    int length = one.getString("content").length();
//                    if (length < min) {
//                        shortJoke = one;
//                        min = length;
//                    }
//                }
//
//            }catch (Exception e){
//                    return;
//                }
//
//            //打印输出得到的笑话，便于调试
//            System.out.println(shortJoke.toJSONString());}

            //注：建议使用主流邮箱，我曾遇到用sohu发出邮件丢失的情况，
            //不仅仅是在程序这里，包括在sohu邮箱客户端测试也发生了邮件丢失
            Properties prop = new Properties();
            prop.setProperty("mail.host", "smtp.163.com");
            prop.setProperty("mail.transport.protocol", "smtp");
            //prop.setProperty("mail.smtp.port","25");
            prop.setProperty("mail.smtp.auth", "true");

            //使用STARTTLS,对于其它协议（如pop3，imap），只需要将smtp改为相应协议即可（pop3要改为pop）
            // 若要使用SSL，只需要设置mail.smtp.ssl.enable为true
            prop.put("mail.smtp.starttls.enable", "true");
            prop.put("mail.smtp.port", "25");

            //使用JavaMail发送邮件的5个步骤
            //1、创建session
            Session session = Session.getInstance(prop);
            //开启Session的debug模式，便于看到发送Email的运行状态
            session.setDebug(true);
            //2、通过session得到transport对象
            Transport ts = session.getTransport();
            //3、发件邮箱的帐号和密码。
            ts.connect("18682635069@163.com", "huayang520");
            //4、创建邮件（Java用Message对象封装（代表）邮件），其中，最后一个参数含义：
            //（收件人<-->RecipientType.TO，抄送<-->RecipientType.CC，密送<-->RecipientType.BBC）
            Message message = createSimpleMail(session, "good morning", realTimeWeather, Message.RecipientType.TO);
            //5、发送邮件
            ts.sendMessage(message, message.getAllRecipients());

            ts.close();
        }
    }

    public static MimeMessage createSimpleMail(Session session, String title, String content, Message.RecipientType type)
            throws Exception {
        //创建MIME邮件对象
        MimeMessage message = new MimeMessage(session);

        //指定发件人昵称
        String nick = MimeUtility.encodeText("张虎", "utf-8", "B");//避免乱码

        //指明邮件的发件人
        message.setFrom(new InternetAddress(nick + "<18682635069@163.com>"));

        //指明邮件的收件人
        //message.addRecipient(type, new InternetAddress("flower.monk@foxmail.com"));
        message.addRecipient(type, new InternetAddress("297043943@qq.com"));
        //message.addRecipient(type,new InternetAddress("724223850@qq.com"));
        //message.addRecipient(type,new InternetAddress("1396964184@qq.com"));

        //邮件主题
        message.setSubject(title);

        //邮件的文本内容，加入一些符号让结尾美美哒(^.^)(^.^)
        message.setContent(content.substring(0, content.length() - 1) + " ==>>(^.^)(^.^)早上好!美美哒", "text/html;charset=UTF-8");

        //返回创建好的邮件对象
        return message;
    }

}
